<?php

/**
 * ECSHOP 商品类型管理程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: goods_type.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

$exc = new exchange($ecs->table("picture_type"), $db, 'picture_id', 'picture_name');

/*------------------------------------------------------ */
//-- 管理界面
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'manage')
{	admin_priv('picture_manage');
    assign_query_info();

    $smarty->assign('ur_here',          "图片属性");
    $smarty->assign('full_page',        1);

    $good_type_list = get_goodstype();
    $good_in_type = '';

    $smarty->assign('goods_type_arr',   $good_type_list['type']);
    $smarty->assign('filter',       $good_type_list['filter']);
    $smarty->assign('record_count', $good_type_list['record_count']);
    $smarty->assign('page_count',   $good_type_list['page_count']);
$sql="SELECT * from ".$GLOBALS['ecs']->table('picture_type');	$query = $db->query("SELECT * from ".$GLOBALS['ecs']->table('picture_type'));
//  $query = $db->query("SELECT a.cat_id FROM " . $ecs->table('attribute') . " AS a RIGHT JOIN " . $ecs->table('goods_attr') . " AS g ON g.attr_id = a.attr_id GROUP BY a.cat_id");
     while ($row = $db->fetchRow($query))
    {
        $good_in_type[$row['cat_id']]=1;
    }
    $smarty->assign('good_in_type', $good_in_type);

    $smarty->assign('action_link',      array('text' => 添加图片属性, 'href' => 'picture_type.php?act=add'));

    $smarty->display('picture_type.htm');
}

/*------------------------------------------------------ */
//-- 获得列表
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $good_type_list = get_goodstype();

    $smarty->assign('goods_type_arr',   $good_type_list['type']);
    $smarty->assign('filter',       $good_type_list['filter']);
    $smarty->assign('record_count', $good_type_list['record_count']);
    $smarty->assign('page_count',   $good_type_list['page_count']);

    make_json_result($smarty->fetch('picture_type.htm'), '',
        array('filter' => $good_type_list['filter'], 'page_count' => $good_type_list['page_count']));
}

/*------------------------------------------------------ */
//-- 修改商品类型名称
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_type_name')
{
    check_authz_json('picture_manage');

    $type_id   = !empty($_POST['id'])  ? intval($_POST['id']) : 0;
    $type_name = !empty($_POST['val']) ? json_str_iconv(trim($_POST['val']))  : '';

    /* 检查名称是否重复 */
    $is_only = $exc->is_only('cat_name', $type_name, $type_id);

    if ($is_only)
    {
        $exc->edit("cat_name='$type_name'", $type_id);

        admin_log($type_name, 'edit', 'goods_type');

        make_json_result(stripslashes($type_name));
    }
    else
    {
        make_json_error($_LANG['repeat_type_name']);
    }
}

/*------------------------------------------------------ */
//-- 切换启用状态
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'toggle_enabled')
{
    check_authz_json('picture_manage');

    $id     = intval($_POST['id']);
    $val    = intval($_POST['val']);

    $exc->edit("enabled='$val'", $id);

    make_json_result($val);
}

/*------------------------------------------------------ */
//-- 添加商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add')
{
    admin_priv('picture_manage');

	$picture_cat_all = $db->getAll("SELECT * from ".$GLOBALS['ecs']->table('category'));	$picture_info_type = array();	    foreach ($picture_cat_all AS $key=>$val)    {    	if (!$db->getAll("SELECT * from ".$GLOBALS['ecs']->table('category'). " where parent_id = ".$val['cat_id']))		{			array_push($picture_info_type,$val);		}	}			$smarty->assign('picture_info_type',   $picture_info_type);
	$val_arr = explode('|', $goods_type['picture_group']);	$radio_arr=get_radio($picture_info_type,$val_arr);		$smarty->assign('val_arr',$radio_arr);		        $smarty->assign('ur_here',     $_LANG['new_picture_type']);

    $smarty->assign('action_link', array('href'=>'picture_type.php?act=manage', 'text' => $_LANG['picture_type_list']));

    $smarty->assign('action',      'add');

    $smarty->assign('form_act',    'insert');

    $smarty->assign('picture_type',  array('enabled' => 1));

    assign_query_info();
    $smarty->display('goods_type_picture.htm');
}

elseif ($_REQUEST['act'] == 'insert')
{	admin_priv('picture_manage');
    //$goods_type['cat_name']   = trim_right(sub_str($_POST['cat_name'], 60));
    //$goods_type['attr_group'] = trim_right(sub_str($_POST['attr_group'], 255));
    $picture_name['picture_name']   = sub_str($_POST['picture_name'], 60);//  $picture_name['picture_group'] = sub_str($_POST['picture_group'], 255);	for($i=0;$i<count($_POST['picture_group']);$i++){			$picture_name['picture_group']=$picture_name['picture_group']."|".$_POST['picture_group'][$i];	}	$picture_name['picture_group']=$picture_name['picture_group']."|";
    $picture_name['enabled']    = intval($_POST['enabled']);
    if ($db->autoExecute($ecs->table('picture_type'), $picture_name) !== false)
    {		
        $links = array(array('href' => 'picture_type.php?act=manage', 'text' => 图片分组列表));				admin_log($picture_name['picture_name'], 'add', 'picturetype');		
        sys_msg('新建图片类型成功', 0, $links);
    }
    else
    {
        sys_msg('新建图片属性成功', 1);
    }
}

/*------------------------------------------------------ */
//-- 编辑商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'edit')
{	admin_priv('picture_manage');
    $goods_type = get_goodstype_info(intval($_GET['picture_id']));

    if (empty($goods_type))
    {
        sys_msg($_LANG['cannot_found_goodstype'], 1);
    }
	$picture_cat_all = $db->getAll("SELECT * from ".$GLOBALS['ecs']->table('category'));	$picture_info_type = array();	    foreach ($picture_cat_all AS $key=>$val)    {    	if (!$db->getAll("SELECT * from ".$GLOBALS['ecs']->table('category'). " where parent_id = ".$val['cat_id']))		{			array_push($picture_info_type,$val);		}	}				$smarty->assign('picture_info_type',   $picture_info_type);	$val_arr = explode('|', $goods_type['picture_group']);	$radio_arr=get_radio($picture_info_type,$val_arr);		$smarty->assign('val_arr',$radio_arr);		
    $smarty->assign('ur_here',     $_LANG['edit_goods_type']);
    $smarty->assign('action_link', array('href'=>'picture_type.php?act=manage', 'text' => 图片分类列表));
    $smarty->assign('action',      'add');
    $smarty->assign('form_act',    'update');
    $smarty->assign('picture_type',  $goods_type);

    assign_query_info();
    $smarty->display('goods_type_picture.htm');
}
elseif ($_REQUEST['act'] == 'update')
{	admin_priv('picture_manage');
    $goods_type['picture_name']   = sub_str($_POST['picture_name'], 60);//  $goods_type['picture_group'] = sub_str($_POST['picture_group'], 255);	for($i=0;$i<count($_POST['picture_group']);$i++){			$goods_type['picture_group']=$goods_type['picture_group']."|".$_POST['picture_group'][$i];	}	$goods_type['picture_group']=$goods_type['picture_group']."|";	
    $goods_type['enabled']    = intval($_POST['enabled']);
    $picture_id                   = intval($_POST['picture_id']);//  $old_groups               = get_picture_groups($picture_id);
    if ($db->autoExecute($ecs->table('picture_type'), $goods_type, 'UPDATE', "picture_id='$picture_id'") !== false)
    {
        /* 对比原来的分组 *///      $new_groups = explode("\n", str_replace("\r", '', $goods_type['picture_group']));  // 新的分组

//      foreach ($old_groups AS $key=>$val)
//
//      {
//
//          $found = array_search($val, $new_groups);
//
//
//
//          if ($found === NULL || $found === false)
//
//          {
//
//              /* 老的分组没有在新的分组中找到 */
//
//              update_attribute_group($picture_id , $key, 0);
//
//          }
//
//          else
//
//          {
//
//              /* 老的分组出现在新的分组中了 */
//
//              if ($key != $found)
//
//              {
//
//                  update_attribute_group($picture_id , $key, $found); // 但是分组的key变了,需要更新属性的分组
//
//              }
//
//          }
//
//      }

        $links = array(array('href' => 'picture_type.php?act=manage', 'text' => 图片分组列表));				admin_log($goods_type['picture_name'], 'edit', 'picturetype');		
        sys_msg(图片属性更新成功, 0, $links);
    }
    else
    {
        sys_msg(图片属性更新失败, 1);
    }
}

/*------------------------------------------------------ */
//-- 删除商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'remove')
{
	check_authz_json('picture_delect');
    $id = intval($_GET['id']);	$sql = "SELECT picture_name FROM " .$ecs->table('picture_type'). " WHERE picture_id = '$id'";    $logo_name = $db->getOne($sql);

    $name = $exc->get_name($id);

    if ($exc->drop($id))

    {
//      admin_log(addslashes($name), 'remove', 'picture_type');
//		 $GLOBALS['db']->query("DELETE FROM " .$ecs->table('picture_type'). " WHERE picture_id =" .$id );
        /* 清除该类型下的所有属性 */
//      $sql = "SELECT attr_id FROM " .$ecs->table('attribute'). " WHERE cat_id = '$id'";
//
//      $arr = $db->getCol($sql);
//
//
//
//      $GLOBALS['db']->query("DELETE FROM " .$ecs->table('attribute'). " WHERE attr_id " . db_create_in($arr));
//
//      $GLOBALS['db']->query("DELETE FROM " .$ecs->table('goods_attr'). " WHERE attr_id " . db_create_in($arr));

        $url = 'picture_type.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
		admin_log($logo_name, 'remove', 'picturetype');
        ecs_header("Location: $url\n");
        exit;
    }
//
    else

    {

        make_json_error($_LANG['remove_failed']);

    }
}
//获取品牌分组复选框function get_radio($pic_cat,$val_arr){	foreach ($pic_cat AS $key => $var){		foreach ($val_arr AS $k => $v)	     {	     	if($var['cat_id']==$v){	     		$radio_arr[$key] = "<div style='width: 15%;float: left;height: 30px;'><input type='checkbox' name='picture_group[]' value='$var[cat_id]' checked>$var[cat_name]</div>";				break;	     	}else{	     		$radio_arr[$key] = "<div style='width: 15%;float: left;height: 30px;'><input type='checkbox' name='picture_group[]' value='$var[cat_id]' >$var[cat_name]</div>";				continue;	     	}	     }	}	return $radio_arr;}
/**
 * 获得所有商品类型
 *
 * @access  public
 * @return  array
 */
function get_goodstype()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('picture_type');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 *///		$sql="SELECT * from ".$GLOBALS['ecs']->table('picture_type');
//      $sql = "SELECT t.*, COUNT(a.cat_id) AS attr_count ".
//
//             "FROM ". $GLOBALS['ecs']->table('goods_type'). " AS t ".
//
//             "LEFT JOIN ". $GLOBALS['ecs']->table('attribute'). " AS a ON a.cat_id=t.cat_id ".
//
//             "GROUP BY t.cat_id " .
//
//             'LIMIT ' . $filter['start'] . ',' . $filter['page_size'];		      $sql = "SELECT t.* ".

               "FROM ". $GLOBALS['ecs']->table('picture_type'). " AS t ".

//             "GROUP BY t.cat_id " .

               'LIMIT ' . $filter['start'] . ',' . $filter['page_size'];
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $all = $GLOBALS['db']->getAll($sql);

    foreach ($all AS $key=>$val)
    {    		  	$sql="SELECT cat_id,cat_name from ".$GLOBALS['ecs']->table('category');
		$all_picture=$GLOBALS['db']->getAll($sql);		$get_cat=explode('|', $val['picture_group']);		$all[$key]['picture_group'] = "";		foreach($get_cat as $k => $v){			foreach($all_picture as $kk => $vv)			{				if ($vv['cat_id'] == $v)				{					$all[$key]['picture_group'] .= $vv['cat_name']." ";					break;				}			}					}        $all[$key]['attr_group'] = strtr($val['attr_group'], array("\r" => '', "\n" => ", "));
    }

    return array('type' => $all, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

/**
 * 获得指定的商品类型的详情
 *
 * @param   integer     $cat_id 分类ID
 *
 * @return  array
 */
function get_goodstype_info($cat_id)
{
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('picture_type'). " WHERE picture_id='$cat_id'";

    return $GLOBALS['db']->getRow($sql);
}

/**
 * 更新属性的分组
 *
 * @param   integer     $cat_id     商品类型ID
 * @param   integer     $old_group
 * @param   integer     $new_group
 *
 * @return  void
 */
//function update_attribute_group($cat_id, $old_group, $new_group)
//
//{
//	
//		
//	echo $cat_id;
//	
//	echo $old_group;
//	echo $new_group;
//  $sql = "UPDATE " . $GLOBALS['ecs']->table('picture_type') .
//
//          " SET picture_group='$new_group' WHERE picture_id='$cat_id' AND picture_group='$old_group'";
//
//  $GLOBALS['db']->query($sql);
//
//}

?>
